package com.example.controller;

import com.example.common.PageBean;
import com.example.common.Result;
import com.example.dto.EmpDTO;
import com.example.entity.Emp;
import com.example.service.EmpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理控制器
 */
@RestController
@RequestMapping("/emps")
@Tag(name = "员工管理", description = "员工信息的增删改查")
public class EmpController {
    
    @Autowired
    private EmpService empService;
    
    /**
     * 分页查询员工
     */
    @GetMapping
    @Operation(summary = "分页查询员工", description = "根据条件分页查询员工信息")
    @Parameters({
        @Parameter(name = "name", description = "员工姓名", example = "张三"),
        @Parameter(name = "deptId", description = "部门ID", example = "1"),
        @Parameter(name = "page", description = "页码", example = "1"),
        @Parameter(name = "pageSize", description = "每页记录数", example = "10")
    })
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "查询成功"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<PageBean<Emp>> page(
            String name,
            Integer deptId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean<Emp> pageBean = empService.page(name, deptId, page, pageSize);
        return Result.success(pageBean);
    }
    
    /**
     * 根据ID查询员工
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询员工", description = "根据员工ID查询员工详细信息")
    @Parameter(name = "id", description = "员工ID", required = true, example = "1")
    public Result<Emp> getById(@PathVariable Integer id) {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    
    /**
     * 新增员工
     */
    @PostMapping
    @Operation(summary = "新增员工", description = "添加新员工信息")
    public Result<String> save(@RequestBody EmpDTO empDTO) {
        empService.save(empDTO);
        return Result.success();
    }
    
    /**
     * 修改员工
     */
    @PutMapping
    @Operation(summary = "修改员工", description = "修改员工信息")
    public Result<String> update(@RequestBody EmpDTO empDTO) {
        empService.update(empDTO);
        return Result.success();
    }
    
    /**
     * 删除员工
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除员工", description = "根据ID删除员工")
    @Parameter(name = "id", description = "员工ID", required = true, example = "1")
    public Result<String> delete(@PathVariable Integer id) {
        empService.delete(id);
        return Result.success();
    }
    
    /**
     * 查询所有员工
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有员工", description = "获取所有员工信息")
    @ApiResponse(responseCode = "200", description = "查询成功", 
                content = @Content(mediaType = "application/json",
                schema = @Schema(implementation = Result.class)))
    public Result<List<Emp>> list() {
        List<Emp> list = empService.list();
        return Result.success(list);
    }
} 